home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / ncsat.cpt / Telnet2.5 final / tcpip / binsubs.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-04-03  |  9.5 KB  |  468 lines

  1. #ifndef lint
  2. static char *SCCSid = "%W%    (NCSA)    %G%";
  3. #endif
  4. /*
  5. *    binsubs.c
  6. *     by Gaige B. Paulsen
  7. ****************************************************************************
  8. *                                                                          *
  9. *      Uses    :                                                               *
  10. *      TCP/IP kernel for NCSA Telnet                                       *
  11. *      by Tim Krauskopf                                                    *
  12. *       with Macintosh code by Gaige B. Paulsen                                 *
  13. *                                                                          *
  14. *      National Center for Supercomputing Applications                     *
  15. *      152 Computing Applications Building                                 *
  16. *      605 E. Springfield Ave.                                             *
  17. *      Champaign, IL  61820                                                *
  18. *                                                                          *
  19. *                                                                          *
  20. ****************************************************************************
  21. *
  22. *    MacBinary Subroutines.    
  23. *
  24. *    Called by:
  25. *        bkgr.c
  26. */
  27.  
  28. #include <Windows.h>
  29. #include <Dialogs.h>
  30. #include <Files.h>
  31. #include <Packages.h>
  32. #include <Memory.h>
  33.  
  34. #include <stdio.h>
  35. #include <fcntl.h>
  36. #include "mpw.h"
  37. #include <string.h>
  38. #include <strings.h>
  39. #include "configrec.h"
  40. #include "maclook.h"
  41. #include "bkgr.h"
  42. #include "MacBinary.h"
  43.  
  44. extern MBFile        /* BYU */
  45.     *ftp_mbfp,        /* BYU */
  46.     *mbfp;            /* BYU */
  47.  
  48. char *strncpy();
  49.  
  50. #define BLOCKS(x)    ((x+127)/128)
  51. #define lmove(f,t)    movmem(f,t,4)
  52.  
  53. #define MyFavouriteTextEditor '????'
  54.  
  55. MBHead
  56.     *mbh;
  57. char 
  58.     buffer[128];
  59.  
  60. char *strsave( p)
  61. char *p;
  62. {
  63.     char *t;
  64.  
  65.     t=NewPtr(strlen(p));
  66.     return strncpy(t,p,strlen(p));
  67. }
  68.  
  69. GetFileInfo(vol,name,iop)
  70. short vol;
  71. char *name;
  72. FileParam *iop;
  73. {
  74.     iop->ioNamePtr = name;
  75.     iop->ioVRefNum=vol;
  76.     iop->ioFVersNum=iop->ioFDirIndex=0;
  77.     PBGetFInfo((ParmBlkPtr) iop, FALSE);
  78. }
  79.  
  80. SetFileInfo(vol,name,iop)
  81. short vol;
  82. char *name;
  83. FileParam *iop;
  84. {
  85.     iop->ioNamePtr = name;
  86.     iop->ioVRefNum=vol;
  87.     iop->ioFVersNum=iop->ioFDirIndex=0;
  88.     PBSetFInfo((ParmBlkPtr) iop, FALSE);
  89. }
  90.  
  91. MakeTextFile(vol,name,iop)
  92. short vol;
  93. char *name;
  94. FileParam *iop;
  95. {    GetFileInfo(vol,name,iop);
  96.     iop->ioFlFndrInfo.fdType = 'TEXT';
  97.     iop->ioFlFndrInfo.fdCreator = MyFavouriteTextEditor;
  98.     SetFileInfo(vol,name,iop);
  99. }
  100.  
  101. isMacBinary(p)
  102. MBHead *p;
  103. {
  104.     return( (p->nlen > 0)   &&
  105.             (p->nlen < 65)  &&
  106.             (p->zero1 == 0) &&
  107.             (p->zero2 == 0) &&
  108.             (p->zero3 == 0));
  109. }
  110.  
  111. int MBsize
  112.   (
  113.     MBFile *mbfp
  114.   )
  115. {
  116.     long int size;
  117.     int ret;
  118.     
  119.     size = 0;
  120.     
  121.     ret = GetEOF( mbfp->fd, &size );            /* length of file data fork */
  122.     if (ret != noErr) 
  123.         size = 0;
  124.  
  125.     return(size);
  126. }
  127.  
  128. MBFile *MBopen( file, vrefnum, mode)
  129. char *file;
  130. short vrefnum,mode;
  131. {
  132.     MBFile *mbfp;
  133.     int err;
  134.  
  135.     if (strlen(file) > 63)             /* BYU */
  136.         return(0L);                    /* BYU */
  137.  
  138. #if 0                                /* BYU 2.4.12 */
  139.     if (mode & MB_WRITE)
  140.         putln("MBOpen for write");
  141.     else 
  142.         putln("MBOpen for read");
  143.  
  144.     if (mode & MB_DISABLE)
  145.         putln("MacBinary Protocol Disabled");
  146. #endif                                /* BYU 2.4.12 */
  147.  
  148.     (Ptr) mbfp = NewPtr( sizeof(MBFile));
  149.     setmem(mbfp, sizeof(MBFile), 0);                    /* BYU 2.4.15 */
  150.     if (mbfp==0L)
  151.         return(0L);
  152.  
  153.     if (strlen(file) > 64)                                /* BYU 2.4.16 */
  154.         movmem( file, mbfp->name, 64);                    /* BYU 2.4.16 */
  155.     else                                                /* BYU 2.4.16 */
  156.         strcpy(mbfp->name, file);                        /* BYU 2.4.16 */
  157.  
  158.     putln(mbfp->name);
  159.     c2pstr(mbfp->name);
  160.     mbfp->vrefnum=vrefnum;
  161.     mbfp->mode = mode;
  162.  
  163.     if ((err=FSOpen( mbfp->name, vrefnum, &mbfp->fd))) {
  164.         if ((err==-43) && (mode & MB_WRITE)) {
  165.             Create( mbfp->name, vrefnum, MyFavouriteTextEditor, 'TEXT');
  166.             if (FSOpen( mbfp->name, vrefnum, &mbfp->fd)) 
  167.                 return( 0L);
  168.             }
  169.         else 
  170.             return(0L);
  171.         }
  172.  
  173.     mbfp->binary=0;
  174.     mbfp->sector1=1;
  175.     mbfp->fork=0;
  176.     return( mbfp);
  177. }
  178.  
  179. int bwrite
  180.   (
  181.     MBFile *out,
  182.     char *buffer,
  183.     int size
  184.   )
  185. {
  186.     long len=size;
  187.     int error;
  188.  
  189.     error = 0;
  190.     
  191.     if (out->binary) {
  192.         if (out->bytes>0) {
  193.             if (out->bytes < len) len = out->bytes;
  194.             error= FSWrite( out->fd, &len, buffer);
  195.             out->bytes -= len;
  196.             buffer +=len;
  197.             size -= (int)len;
  198.             }
  199.         if (out->bytes<= 0) {
  200.             if (!out->fork) {
  201.                 out->fork=1;
  202.                 out->bytes=BLOCKS(out->rlen)*128;
  203.                 SetEOF( out->fd, (long) out->dlen);
  204.                 FSClose( out->fd);
  205.                 if (out->bytes) {
  206.                     OpenRF( out->name, out->vrefnum,&out->fd);
  207.                     if (size) {
  208.                         len = (long) size;
  209.                         error= FSWrite( out->fd, &len, buffer);
  210.                         }
  211.                     }
  212.                 else
  213.                     out->fd=0;
  214.                 }
  215.             else SetEOF( out->fd, (long) out->rlen);
  216.             }
  217.         }
  218.     else {
  219.         error = FSWrite( out->fd, &len, buffer);
  220.         }
  221.     return (error);
  222. }
  223.  
  224. void ProcessMBHead
  225.   (
  226.     MBFile *out,
  227.     MBHead *header
  228.   )
  229. {
  230.     int err;
  231.  
  232.     movmem( header, &out->header, sizeof(MBHead));
  233.     out->binary=1;
  234.     lmove( &header->dflen[0], &out->dlen);
  235.     lmove( &header->rflen[0], &out->rlen);
  236.     out->bytes= BLOCKS(out->dlen)*128;
  237.     out->fork=0;
  238.     out->sector1=0;
  239.  
  240.     FSClose(out->fd);
  241.     if (FSDelete( out->name, out->vrefnum))
  242.         putln("Error Deleting Old File ");
  243.         
  244.     movmem( &out->header.nlen, out->name,63);
  245.     
  246. #if 0                                                            /* BYU 2.4.17 */
  247.     MBstat( &out->header.nlen, 1, (int)(BLOCKS(out->dlen)+BLOCKS(out->rlen)) );
  248. #endif                                                            /* BYU 2.4.17 */
  249.  
  250.     if (out->bytes) {
  251.         if ((err=FSOpen( out->name, out->vrefnum, &out->fd))) {
  252.             if (err=-43) {
  253.                 long cre,typ;
  254.  
  255. /*  this crashes Mac Pluses                
  256.                 typ = *(long *)out->header.type;
  257.                 cre = *(long *)out->header.creator;
  258. */
  259.                 lmove(out->header.type, &typ);
  260.                 lmove(out->header.creator, &cre);
  261.  
  262.                 Create( out->name, out->vrefnum, cre,typ);
  263.                 if (FSOpen( out->name, out->vrefnum, &out->fd)) 
  264.                     return;
  265.                 }
  266.             else {
  267.                 return;
  268.                 }
  269.             }
  270.         }
  271.     else {
  272.         if ((err=OpenRF( out->name, out->vrefnum, &out->fd))) {
  273.             if (err=-43) {
  274.                 long cre,typ;
  275.                 
  276. /*  this crashes Mac Pluses                
  277.                 typ = *(long *)out->header.type;
  278.                 cre = *(long *)out->header.creator;
  279. */
  280.                 lmove(out->header.type, &typ);
  281.                 lmove(out->header.creator, &cre);
  282.  
  283.                 Create( out->name, out->vrefnum, cre,typ);
  284.                 if (OpenRF( out->name, out->vrefnum, &out->fd)) 
  285.                     return;
  286.                 }
  287.             else {
  288.                 return;
  289.                 }
  290.             }
  291.         out->fork = 1;
  292.         out->bytes=BLOCKS(out->rlen)*128;
  293.         }
  294. }
  295.  
  296. int MBwrite
  297.   (
  298.     MBFile *out,
  299.     void *buffer,
  300.     int size
  301.   )
  302. {
  303.     int rsize;
  304.     
  305.     if (size < 1)
  306.         return(0);
  307.  
  308.     rsize=size;
  309.  
  310.     if (out->sector1 && (size >= sizeof(struct MBHead)) &&
  311.         isMacBinary(buffer) && (!(out->mode & MB_DISABLE))) {
  312.         putln("First sector of MacBinary file");
  313.         ProcessMBHead( out, (MBHead *) buffer);
  314.         (Ptr) buffer+=128;
  315.         if ((size-=128) <1)
  316.             return(rsize);
  317.         }
  318.  
  319.     if (bwrite( out,buffer,size))
  320.         return(-1);
  321.     else
  322.         return( rsize);
  323. }
  324.  
  325. void MBclose
  326.   (
  327.     MBFile *out
  328.   )
  329. {
  330.     FileParam finfo;
  331.     long fpos;
  332.  
  333.     putln("MBclose");
  334.     
  335.     if (!out->fd)
  336.         return;
  337.  
  338.     if (!(out->mode & MB_DISABLE) && (out->mode & MB_WRITE)) {
  339.         if (out->fork)
  340.             SetEOF( out->fd, (long) out->rlen);
  341.         else
  342.             SetEOF( out->fd, (long) out->dlen);
  343.  
  344.         FSClose( out->fd);
  345.         GetFileInfo( 0, out->name, &finfo);
  346.     
  347.         movmem( &out->header.type[0], &finfo.ioFlFndrInfo, sizeof(FInfo));
  348.         lmove( &out->header.cdate[0], &finfo.ioFlCrDat);
  349.         lmove( &out->header.mdate[0], &finfo.ioFlMdDat);
  350.         finfo.ioFlFndrInfo.fdFlags &= 0xfeff;
  351.         finfo.ioFlRLgLen=out->rlen;
  352.         finfo.ioFlLgLen =out->dlen;
  353.     
  354.         SetFileInfo( 0, out->name, &finfo);
  355.         }
  356.     else if (out->mode & MB_WRITE) {
  357.         GetFPos( out->fd, &fpos);
  358.         SetEOF(  out->fd,  fpos);
  359.         FSClose( out->fd);
  360.         }
  361.     else
  362.         FSClose( out->fd);
  363. }
  364.  
  365. int MBread
  366.   (
  367.     MBFile *in,
  368.     void *buffer,
  369.     int size
  370.   )
  371. {
  372.     char *p;
  373.     int rsize=size;
  374.  
  375.  
  376.     if (in->fork<0) {
  377.         return(-1);
  378.         }
  379.  
  380.     p=buffer;
  381.     if (in->sector1) {
  382.         FileParam finfo;
  383.  
  384.         setmem( &in->header, sizeof(MBHead), 0);
  385.         movmem(  in->name, &in->header.nlen, 64);
  386.         GetFileInfo( in->vrefnum, in->name, &finfo);
  387.         movmem( &finfo.ioFlFndrInfo, &in->header.type[0], sizeof(FInfo) );
  388.         in->header.protected = (in->header.zero2 & 0x40)?1:0;
  389.         in->header.zero2 = 0;
  390.         lmove( &finfo.ioFlLgLen, &in->header.dflen[0]);
  391.         lmove( &finfo.ioFlRLgLen,&in->header.rflen[0]);
  392.         lmove( &finfo.ioFlCrDat, &in->header.cdate[0]);
  393.         lmove( &finfo.ioFlMdDat, &in->header.mdate[0]);
  394.         in->dlen=finfo.ioFlLgLen;
  395.         in->rlen=finfo.ioFlRLgLen;
  396.         if (! (in->mode & MB_DISABLE) ) {
  397.             if (size<128) return(-1);
  398.  
  399.             movmem( &in->header, p, 128);
  400.             p +=128;
  401.             size -= 128;
  402.             in->bytes= BLOCKS(in->dlen)*128;
  403.             in->binary=1;
  404.             }
  405.         else {
  406.             in->bytes = in->dlen;
  407.             in->rlen=0;
  408.             in->binary=0;
  409.             }
  410.         in->sector1=0;
  411. #if 0                                                            /* BYU 2.4.17 */
  412.         MBstat( &in->header.nlen, 1, (int) (BLOCKS(in->dlen)+BLOCKS(in->rlen)) );
  413. #endif                                                            /* BYU 2.4.17 */
  414.         }
  415.  
  416.     if ( size >0) {
  417.         long length = (long)size;
  418.         int err;
  419.  
  420.         err = FSRead( in->fd, &length, p);
  421.  
  422.         size -=(int)length;
  423.         in->bytes -=length;
  424.         p += length;
  425.  
  426.         if (err == -39 || (in->bytes<=0) ) {
  427.             FSClose( in->fd );
  428.             if (in->bytes<0L) in->bytes=0L;
  429.             size -= (int)in->bytes;
  430.             p    +=      in->bytes;                /* Make adjustments for necessary 128 byte term */
  431.             if (!in->fork ) {
  432.                 in->fork=1;
  433.                 in->bytes= BLOCKS(in->rlen)*128;
  434.                 if (in->bytes) {
  435.                     OpenRF( in->name, in->vrefnum, &in->fd);
  436. #ifdef READ
  437.                     length=(long)size;
  438.                     if (length >0L) {
  439.                         err = FSRead( in->fd, &length, p);
  440.                         size -= (int)length;
  441.                         in->bytes -=length;
  442.                         }
  443. #endif READ
  444.                     }
  445.                 else {
  446.                     in->fd=0;
  447.                     in->fork=-1;                    /* Time to close up shop */
  448.                     }
  449.                 }
  450.             else {
  451.                 in->fd=0;
  452.                 in->fork=-1;                    /* Time to close up shop */
  453.                 }
  454.             }
  455.         }
  456.     return( rsize-size); 
  457. }
  458.  
  459. init_mb_files() {            /* BYU */
  460.   ftp_mbfp->fd = 0;            /* BYU */
  461.   mbfp->fd = 0;                /* BYU */
  462. }                            /* BYU */
  463.                             /* BYU */
  464. close_mb_files() {            /* BYU */
  465.     if (ftp_mbfp->fd != 0) MBclose( ftp_mbfp );        /* BYU - close input file */
  466.     if (mbfp->fd != 0) MBclose( mbfp );                /* BYU - close input file */
  467. }                            /* BYU */
  468.